var ARR_ACTIVEX = ["MSXML4.DOMDocument", "MSXML3.DOMDocument", "MSXML2.DOMDocument", "MSXML.DOMDocument", "Microsoft.XmlDom"];
var STR_ACTIVEX = "";

var isIE = navigator.userAgent.toLowerCase().indexOf("msie") > -1;
var isMoz = document.implementation && document.implementation.createDocument;

if (isIE) { //Internet Explorer
    var bFound = false;
    for (var i=0; i < ARR_ACTIVEX.length && !bFound; i++) {
        try {
            var objXML = new ActiveXObject(ARR_ACTIVEX[i]); //Find appropriate MSXML DOM Document
            STR_ACTIVEX = ARR_ACTIVEX[i];
            bFound = true
        } catch (objException) { }
    }
    if (!bFound) throw "No DOM DOcument found on your computer."
}

if (isMoz) { //Mozilla
    Document.prototype.loadXML = function(strXML) {
        changeReadyState(this, 1);
        var objDOMParser = new DOMParser();
        var objDoc = objDOMParser.parseFromString(strXML, "text/xml");
        while (this.hasChildNodes()) this.removeChild(this.lastChild);
        for (var i=0; i < objDoc.childNodes.length; i++) {
            var objImportedNode = this.importNode(objDoc.childNodes[i], true);
            this.appendChild(objImportedNode);
        }
        handleOnLoad(this);
    }
    Node.prototype.__defineGetter__("xml", _Node_getXML);
    Document.prototype.readyState = "0";
    Document.prototype.__load__ = Document.prototype.load;
    Document.prototype.load = _Document_load;
    Document.prototype.onreadystatechange = null;
    Document.prototype.parseError = 0;
}

function changeReadyState(objDOMDocument, iReadyState) {
    objDOMDocument.readyState = iReadyState;
    if (objDOMDocument.onreadystatechange != null && typeof objDOMDocument.onreadystatechange == "function")
        objDOMDocument.onreadystatechange();
}

function jsXML() { }

jsXML.createDOMDocument = function(strNamespaceURI, strRootTagName) {
    var objDOM = null;
    if (isMoz) { //Mozilla
        objDOM = document.implementation.createDocument(strNamespaceURI, strRootTagName, null);
        objDOM.addEventListener("load", _Document_onload, false);
    } else if (isIE) { //Internet Explorer
        objDOM = new ActiveXObject(STR_ACTIVEX);
        if (strRootTagName) {
            if (strNamespaceURI) objDOM.loadXML("<a0:" + strRootTagName + " xmlns:a0=\"" + strNamespaceURI + "\" />");
            else objDOM.loadXML("<" + strRootTagName + "/>");
        }
    }
    return objDOM;
}

function _Document_load(strURL) {
    this.parseError = 0;
    changeReadyState(this, 1);
    try {
        this.__load__(strURL);
    } catch (objException) {
        this.parseError = -9999999;
        changeReadyState(this, 4);
    }
}

function _Document_onload() { handleOnLoad(this); }

function handleOnLoad(objDOMDocument) {
    if (!objDOMDocument.documentElement || objDOMDocument.documentElement.tagName == "parsererror")
        objDOMDocument.parseError = -9999999;
    changeReadyState(objDOMDocument, 4);
}

function _Node_getXML() {
    var objXMLSerializer = new XMLSerializer;
    var strXML = objXMLSerializer.serializeToString(this);
    return strXML;
}